layui.use(['laydate','form','upload'], function(){
    var laydate = layui.laydate;
    var form = layui.form;
    var layer = layui.layer;
    var upload = layui.upload;

    /*自定义验证规则*/
    form.verify({
        isphone: function(value) {
            var phone = /^1[3456789][0-9]{9}$/
            if (value.length > 0 && !phone.test(value)) {
                return '请输入正确的电话号码';
            }
        },
    });
    /*点击提交*/
    form.on('submit(update)',function(data) {
        var edit_url = window.location.href;
        $.post(edit_url,data.field,function(msg){
            if(msg.code === 1){
                layer.msg("编辑成功", {icon: 6, time: 1000}, function(){
                    /*刷新当前页*/
                    location.reload()
                    /*重载父级表格*/
                    parent.layui.table.reload("table-list");
                })
            }else{
                layer.msg(msg.msg, {icon: 2, time: 2000})
            }
        })
        return false;
    })
    /*上传头像*/
    var uploadInst = upload.render({
        elem: '#upload-box', //绑定元素
        url: '/file/file/file_upload', //上传接口
        data: {
            field: 'user_img',
        },
        done: function(res, index, upload){
            /*上传完毕回调*/
            if(res.code === 1){
                $('#upload-box img').attr('src',res.msg)
                $('input[name="user_img"]').val(res.msg)
            }else{
                layer.msg(res.msg, {icon: 2, time: 2000})
            }
        },
        error: function(){
            //请求异常回调
            return layer.msg('请求失败，请重试!',{icon: 2, time: 2000})
        }
    });
    /*@点击编辑*/
    update_user = function (obj){
        obj.style.display = 'none';
        $('.user-info').hide();
        $('.update-box').css('display','inline-block');
    }

})